/****************************************************************************
@FILENAME:	codec_v8t04a.h
@FUNCTION:	decode v8t04a
@AUTHOR:	flege.zhang
@DATE:		2020.03.31
*****************************************************************************/


#ifndef _CODEC_V8T04A__H_
#define _CODEC_V8T04A__H_

#include "app_cfg.h"



//using tccr to decode
#define VALID_SIGNAL_TCCR_TIMEOUT					(0xFFFFFF - 0x75300)//16M/2,unit:0.0625us/2 = 0.125us,45ms = 0x57E40,50ms = 0x61A80,60ms = 0x75300,500ms = 0x3D0900
#define SEND_DATA_THRESHOLD							(0x04)

#define HEAD_RELAX_THRESHOLD						(500)//us
#define DATA_RELAX_THRESHOLD						(100)//us

#define IS_V8T04A_HEADER(timeL,timeH)				(((timeL > (3500 - HEAD_RELAX_THRESHOLD)) && (timeL < (3500 + HEAD_RELAX_THRESHOLD))) && \
													((timeH > (2000 - HEAD_RELAX_THRESHOLD)) && (timeH < (2000 + HEAD_RELAX_THRESHOLD))))
#define IS_V8T04A_BIT1(timeL,timeH)					(((timeL > (370 - DATA_RELAX_THRESHOLD)) && (timeL < (370 + DATA_RELAX_THRESHOLD))) && \
													 ((timeH > (740 - DATA_RELAX_THRESHOLD)) && (timeH < (740 + DATA_RELAX_THRESHOLD))))
#define IS_V8T04A_BIT0(timeL,timeH)					(((timeL > (370 - DATA_RELAX_THRESHOLD)) && (timeL < (370 + DATA_RELAX_THRESHOLD))) && \
													 ((timeH > (370 - DATA_RELAX_THRESHOLD)) && (timeH < (370 + DATA_RELAX_THRESHOLD))))


#if(HW_DETECT_TYPE == DECODE_V8T04A_TYPE)
#define DECODE_DATA_BIT_NUM							(0x08)
#define DECODE_DATA_CHECK_VALUE						(0xA0)
#define DECODE_DATA_1S_COUNT						(16)
#elif(HW_DETECT_TYPE == DECODE_V8T04A_16_TYPE)
#define DECODE_DATA_BIT_NUM							(0x10)
#define DECODE_DATA_CHECK_VALUE						(0xA003)
#define DECODE_DATA_1S_COUNT						(13)
#endif

#define DECODE_DATA_2S_COUNT						(DECODE_DATA_1S_COUNT + (DECODE_DATA_1S_COUNT>>1))
#define DECODE_DATA_3S_COUNT						(DECODE_DATA_1S_COUNT*3)
#define DECODE_DATA_5S_COUNT						(DECODE_DATA_1S_COUNT*5)
#define DECODE_DATA_10S_COUNT						(DECODE_DATA_1S_COUNT*10)
#define DECODE_DATA_20S_COUNT						(DECODE_DATA_1S_COUNT*20)


enum en_dec_v8t04a_state
{
	DEC_V8T04A_STATE_HEADER 	= 0x01,
	DEC_V8T04A_STATE_DATA 		= 0x02,
	DEC_V8T04A_STATE_END 		= 0x03,
};


void decode_v8t04a_init();
void decode_v8t04a(uint16_t time_low,uint16_t time_high);
void decode_v8t04a_end(void);

#endif
